﻿
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using KeRui.Common;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;

    /// <summary>
    /// WapHelper 的摘要说明
    /// </summary>
    public class WapHelper
    {
        private static RegexOptions regex = RegexOptions.IgnoreCase | RegexOptions.Multiline;

        public WapHelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 获得文章列表
        /// </summary>
        public List<KeRui.Model.CmsContent> GetCmsContentList(int NodeID, int IncludeChild, int PageSize, int PageIndex)
        {
            KeRui.BLL.CmsNode bllNode = new KeRui.BLL.CmsNode();
            KeRui.Model.CmsNode modelNode = bllNode.GetModel(NodeID);
            if (modelNode == null)
                return null;
            if (modelNode.allChildID == "")
                return null;
            KeRui.BLL.Common bllCommon = new KeRui.BLL.Common();
            KeRui.BLL.CmsContent bllContent = new KeRui.BLL.CmsContent();
            string strOrder = "PublishTime DESC";
            string strWhere = "IsDelete='False' AND PublicState=1 AND ContentState=2 AND ";
            if (IncludeChild == 1)
                strWhere += "NodeID in(" + modelNode.allChildID + ")";
            else
                strWhere += "NodeID=" + NodeID;
            DataSet ds = bllCommon.GetList("CmsContent", PageSize, PageIndex, strWhere, strOrder);
            return bllContent.DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 获得文章列表
        /// </summary>
        public string GetCmsContentList(int NodeID, int IncludeChild, int PageSize, int PageIndex, int TextCutLen, string TextTemplate)
        {
            return GetCmsContentList(NodeID, IncludeChild, PageSize, PageIndex, TextCutLen, TextTemplate, 0, "");
        }
        public string GetCmsContentList(int NodeID, int IncludeChild, int PageSize, int PageIndex, int TextCutLen, string TextTemplate, int eachNum, string eachSplit)
        {
            string ret = "";
            int i = 0;
            foreach (KeRui.Model.CmsContent model in GetCmsContentList(NodeID, IncludeChild, PageSize, PageIndex))
            {
                if (eachNum > 0 && i != 0 && i % eachNum == 0)
                    ret = ret + eachSplit;
                string title = model.Title;
                if (TextCutLen > 0)
                    title = Utils.cutTrueLength(model.Title, TextCutLen);
                ret += TextTemplate.Replace("@contentid", model.ContentID.ToString()).Replace("@title", title);
                i++;
            }
            return ret;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public string TransHtml(string html)
        {

            MatchCollection matches = null;
            GroupCollection Items = null;

            //werwerwer{$getcmscontentlist,1310,1,10,1,26,<a href="@contentid">@title</a>}werwerewrwer{$getcmscontentlist,1300,2,10,1,29,<a href={contentid}>{title}</a>}ewrwerewr
            if (html.Contains("{$getcmscontentlist"))
            {
                try
                {
                    matches = Regex.Matches(html, @"{\$getcmscontentlist,(?<nodeid>[0-9]*?),(?<includechild>[0-9]*?),(?<pagesize>[0-9]*?),(?<pageindex>[0-9]*?),(?<titlelen>[0-9]*?),(?<texttemplate>.*?)}", regex);
                }
                catch (Exception) { }
                if (matches.Count != 0)
                {
                    foreach (Match match in matches)
                    {
                        Items = match.Groups;
                        try
                        {
                            html = html.Replace("{$getcmscontentlist," + Items["nodeid"].Value + "," + Items["includechild"].Value + "," + Items["pagesize"].Value + "," + Items["pageindex"].Value + "," + Items["titlelen"].Value + "," + Items["texttemplate"].Value + "}"
                            , GetCmsContentList(Utils.StrToInt(Items["nodeid"].Value, 0), Utils.StrToInt(Items["includechild"].Value, 0), Utils.StrToInt(Items["pagesize"].Value, 0), Utils.StrToInt(Items["pageindex"].Value, 0), Utils.StrToInt(Items["titlelen"].Value, 0), Items["texttemplate"].Value));
                        }
                        catch (Exception) { }
                    }
                }
            }

            return html;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string FormatWap(string str)
        {
            //return "liuliu";
            str = str.Replace("<", "&lt;");
            str = str.Replace(">", "&gt;");
            //str = str.Replace("‘", "&apos;");
            str = str.Replace("\"", "&quot;");
            str = str.Replace("&", "&amp;");
            //str = str.Replace("$", "$$");
            str = str.Replace(" ", "&nbsp;");
            //str = str.Replace("-", "&shy;");
            return str;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="nodeid"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetLocation(int nodeid, string url)
        {
            KeRui.BLL.CmsNode bll = new KeRui.BLL.CmsNode();
            KeRui.Model.CmsNode model = bll.GetModelByCache(nodeid);
            if (model == null)
                return "";
            else
            {
                if (model.ParentID != 0)
                    return GetLocation(model.ParentID, url) + "&gt;<a href=\"" + url + model.NodeID + ".wml\">" + model.NodeName + "</a>";
                else
                    return "";
            }
        }


    }
